<template>
  <div class="login-container">
    <!-- 左侧信息面板 -->
    <div class="info-panel">
      <div class="info-content">
        <div class="logo-section">
          <div class="logo-icon">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M12 2L3 7V17L12 22L21 17V7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M12 22V12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M21 7L12 12L3 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </div>
          <h1>心理思维训练系统</h1>
          <p class="logo-subtitle">Psychology Mind Training System</p>
        </div>
        


      </div>
    </div>
    
    <!-- 右侧登录面板 -->
    <div class="login-panel">
      <div class="login-card">
        <!-- 移动端标题和图标 -->
        <div class="mobile-logo">
          <div class="logo-icon">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M12 2L3 7V17L12 22L21 17V7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M12 22V12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M21 7L12 12L3 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </div>
          <h1>心理思维训练系统</h1>
        </div>
        
      
      <!-- 登录表单 -->
      <a-form
        v-if="isLoginMode"
        :model="loginForm"
        :rules="loginRules"
        layout="vertical"
        class="auth-form"
      >
        <div class="form-group">
          <label class="form-label">用户名</label>
          <div class="input-wrapper">
            <IconUser class="input-icon" />
            <a-input 
              v-model="loginForm.username" 
              placeholder="请输入用户名"
              size="large"
              class="custom-input"
            />
          </div>
        </div>
        
        <div class="form-group">
          <label class="form-label">密码</label>
          <div class="input-wrapper">
            <IconLock class="input-icon" />
            <a-input-password 
              v-model="loginForm.password" 
              placeholder="请输入密码"
              size="large"
              class="custom-input"
              @press-enter="handleLogin"
            />
          </div>
        </div>
        
        <a-button 
          type="primary" 
          size="large" 
          long 
          :loading="loading"
          @click="handleLogin"
          class="submit-btn"
        >
          {{ loading ? '登录中...' : '登录' }}
        </a-button>
      </a-form>
      
      <!-- 注册表单 -->
      <a-form
        v-else
        :model="registerForm"
        :rules="registerRules"
        layout="vertical"
        class="auth-form"
        ref="registerFormRef"
      >
        <div class="form-group">
          <label class="form-label">用户名</label>
          <div class="input-wrapper">
            <IconUser class="input-icon" />
            <a-input 
              v-model="registerForm.username" 
              placeholder="请输入用户名"
              size="large"
              class="custom-input"
            />
          </div>
        </div>
        
        <div class="form-group">
          <label class="form-label">密码</label>
          <div class="input-wrapper">
            <IconLock class="input-icon" />
            <a-input-password 
              v-model="registerForm.password" 
              placeholder="请输入密码"
              size="large"
              class="custom-input"
            />
          </div>
        </div>
        
        <div class="form-group">
          <label class="form-label">真实姓名</label>
          <div class="input-wrapper">
            <IconIdcard class="input-icon" />
            <a-input 
              v-model="registerForm.real_name" 
              placeholder="请输入真实姓名"
              size="large"
              class="custom-input"
            />
          </div>
        </div>
        
        <div class="form-row">
          <div class="form-group half">
            <label class="form-label">年龄</label>
            <a-input-number 
              v-model="registerForm.age" 
              placeholder="年龄"
              size="large"
              :min="10"
              :max="100"
              class="custom-input"
              style="width: 100%"
            />
          </div>
          
          <div class="form-group half">
            <label class="form-label">性别</label>
            <a-radio-group v-model="registerForm.gender" size="large" class="gender-group">
              <a-radio value="男">男</a-radio>
              <a-radio value="女">女</a-radio>
            </a-radio-group>
          </div>
        </div>
        
        <a-button 
          type="primary" 
          size="large" 
          long 
          :loading="loading"
          @click="handleRegister"
          class="submit-btn"
        >
          {{ loading ? '注册中...' : '创建账户' }}
        </a-button>
      </a-form>
      
        <!-- 切换登录/注册 -->
        <div class="auth-switch">
          <span class="switch-text">
            {{ isLoginMode ? '还没有账户？' : '已有账户？' }}
          </span>
          <a-button 
            type="text" 
            @click="toggleMode"
            class="switch-btn"
          >
            {{ isLoginMode ? '立即注册' : '立即登录' }}
          </a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import { IconUser, IconLock, IconIdcard } from '@arco-design/web-vue/es/icon'
import { useUserStore } from '../stores/user'
import { userApi } from '../api'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const isLoginMode = ref(true)
const registerFormRef = ref()

// 登录表单
const loginForm = reactive({
  username: '',
  password: ''
})

// 注册表单
const registerForm = reactive({
  username: '',
  password: '',
  real_name: '',
  age: undefined as number | undefined,
  gender: ''
})

// 登录验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名' }
  ],
  password: [
    { required: true, message: '请输入密码' }
  ]
}

// 注册验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名' },
    { minLength: 3, message: '用户名至少3个字符' },
    { maxLength: 20, message: '用户名最多20个字符' }
  ],
  password: [
    { required: true, message: '请输入密码' },
    { minLength: 6, message: '密码至少6个字符' }
  ],
  real_name: [
    { required: true, message: '请输入真实姓名' },
    { minLength: 2, message: '姓名至少2个字符' }
  ],
  age: [
    { required: true, message: '请输入年龄' },
    { 
      validator: (value: number) => {
        if (!value || value < 10 || value > 100) {
          return '年龄必须在10-100岁之间'
        }
        return true
      }
    }
  ],
  gender: [
    { required: true, message: '请选择性别' }
  ]
}

// 切换登录/注册模式
const toggleMode = () => {
  console.log('切换模式，当前模式:', isLoginMode.value ? '登录' : '注册')
  isLoginMode.value = !isLoginMode.value
  // 清空表单
  Object.assign(loginForm, { username: '', password: '' })
  Object.assign(registerForm, { 
    username: '', 
    password: '', 
    real_name: '', 
    age: undefined, 
    gender: '' 
  })
  console.log('切换到模式:', isLoginMode.value ? '登录' : '注册')
}

// 处理登录
const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    Message.error('请填写完整信息')
    return
  }
  
  loading.value = true
  
  try {
    const result = await userStore.login(loginForm.username, loginForm.password)
    
    if (result.success) {
      Message.success('登录成功')
      
      // 根据用户类型跳转
      if (userStore.isAdmin) {
        router.push('/admin')
      } else {
        router.push('/user')
      }
    } else {
      Message.error(result.message || '登录失败')
    }
  } catch (error) {
    Message.error('网络错误，请重试')
  } finally {
    loading.value = false
  }
}

// 处理注册
const handleRegister = async (e?: Event) => {
  e?.preventDefault()
  console.log('注册按钮被点击')
  console.log('当前表单数据:', registerForm)
  
  try {
    // 表单验证
    console.log('开始表单验证...')
    const validation = await registerFormRef.value?.validate()
    console.log('表单验证结果:', validation)
  } catch (error) {
    console.log('表单验证失败:', error)
    Message.error('请检查表单填写是否正确')
    return
  }
  
  loading.value = true
  console.log('开始发送注册请求...')
  
  try {
    const registerData = {
      username: registerForm.username,
      password: registerForm.password,
      real_name: registerForm.real_name,
      age: registerForm.age!,
      gender: registerForm.gender
    }
    console.log('注册数据:', registerData)
    
    const result = await userApi.register(registerData)
    console.log('注册响应:', result)
    
    if (result.success) {
      Message.success('注册成功！请使用新账户登录')
      // 自动切换到登录模式，并填入用户名
      loginForm.username = registerForm.username
      toggleMode()
    } else {
      Message.error(result.message || '注册失败')
    }
  } catch (error) {
    console.error('注册请求错误:', error)
    Message.error('网络错误，请重试')
  } finally {
    loading.value = false
  }
}

// 页面加载时检查是否已登录
const checkLoginStatus = async () => {
  const result = await userStore.restoreUser()
  if (result.success && userStore.isLoggedIn) {
    if (userStore.isAdmin) {
      router.push('/admin')
    } else {
      router.push('/user')
    }
  }
}

checkLoginStatus()
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #2c3b7e 0%, #3533d3 100%);
}

/* 左侧信息面板 */
.info-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 60px;
  color: white;
  position: relative;
  z-index: 1;
}

@media (max-width: 1024px) {
  .info-panel {
    display: none;
  }
}

.info-content {
  max-width: 500px;
}

.logo-section {
  text-align: center;
  margin-bottom: 60px;
}

.logo-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  backdrop-filter: blur(10px);
  color: white;
}

.logo-icon svg {
  width: 40px;
  height: 40px;
}

.logo-section h1 {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 8px 0;
  background: linear-gradient(135deg, #fff 0%, rgba(255,255,255,0.8) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.logo-subtitle {
  font-size: 16px;
  opacity: 0.8;
  margin: 0;
  font-weight: 300;
  letter-spacing: 1px;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  gap: 20px;
}

.feature-icon {
  font-size: 32px;
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  backdrop-filter: blur(10px);
  flex-shrink: 0;
}

.feature-text h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px 0;
  color: white;
}

.feature-text p {
  font-size: 14px;
  opacity: 0.8;
  margin: 0;
  line-height: 1.6;
}

/* 右侧登录面板 */
.login-panel {
  flex: 0 0 480px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  z-index: 1;
}

/* 移动端标题样式 */
.mobile-logo {
  display: none;
  text-align: center;
  margin-bottom: 32px;
}

.mobile-logo .logo-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
  border-radius: 16px;
  color: white;
}

.mobile-logo .logo-icon svg {
  width: 32px;
  height: 32px;
}

.mobile-logo h1 {
  font-size: 24px;
  font-weight: 700;
  margin: 0;
  color: #1a1a1a;
  background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

@media (max-width: 1024px) {
  .login-panel {
    flex: 1;
    padding: 20px;
  }
  
  .mobile-logo {
    display: block;
  }
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0 0 8px 0;
}

.login-header p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* 表单样式 */
.auth-form {
  margin-bottom: 32px;
}

.form-group {
  margin-bottom: 24px;
}

.form-row {
  display: flex;
  gap: 16px;
}

.form-group.half {
  flex: 1;
  margin-bottom: 24px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 16px;
  z-index: 2;
  color: #999;
  font-size: 16px;
}

.custom-input {
  padding-left: 48px !important;
  border: 2px solid #e6e8eb;
  border-radius: 12px;
  font-size: 14px;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.custom-input:hover {
  border-color: #4a90e2;
  background: white;
}

.custom-input:focus {
  border-color: #4a90e2;
  background: white;
  box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}

.gender-group {
  margin-top: 8px;
}

.gender-group .arco-radio {
  margin-right: 24px;
}

.submit-btn {
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
  border: none;
  margin-top: 8px;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(74, 144, 226, 0.3);
}

/* 切换按钮 */
.auth-switch {
  text-align: center;
  margin: 32px 0;
  padding: 24px 0;
  border-top: 1px solid #f0f0f0;
}

.switch-text {
  color: #666;
  font-size: 14px;
  margin-right: 8px;
}

.switch-btn {
  color: #4a90e2;
  font-weight: 500;
  padding: 0;
  font-size: 14px;
  transition: all 0.3s ease;
}

.switch-btn:hover {
  color: #357abd;
}

/* 测试账号信息 */
.login-footer {
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #f0f0f0;
}

.test-accounts {
  text-align: center;
}

.footer-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin: 0 0 16px 0;
}

.account-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  margin: 8px 0;
  background: #f8f9fa;
  border-radius: 8px;
  font-size: 13px;
}

.account-type {
  color: #4a90e2;
  font-weight: 500;
}

.account-info {
  color: #666;
  font-family: 'Consolas', 'Monaco', monospace;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-panel {
    padding: 20px;
  }
  
  .mobile-logo {
    margin-bottom: 24px;
  }
  
  .mobile-logo h1 {
    font-size: 20px;
  }
  
  .mobile-logo .logo-icon {
    width: 50px;
    height: 50px;
    margin-bottom: 12px;
  }
  
  .mobile-logo .logo-icon svg {
    width: 26px;
    height: 26px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
  
  .form-row {
    flex-direction: column;
    gap: 0;
  }
  
  .form-group.half {
    flex: none;
  }
}
</style>

